package app.msy.cn.rxandroid.presenter;


import app.msy.cn.rxandroid.bean.LoginResp;
import app.msy.cn.rxandroid.listener.OnLoginListener;
import app.msy.cn.rxandroid.model.ILoginModel;
import app.msy.cn.rxandroid.model.LoginModelImpl;
import app.msy.cn.rxandroid.ui.view.ILoginView;
import rx.Observable;
import rx.Subscriber;

public class LoginPresenterImpl implements ILoginPresenter {

    private ILoginView loginView;
    private ILoginModel loginModel;

    public LoginPresenterImpl(ILoginView mLoginView) {
        this.loginView = mLoginView;
        loginModel = new LoginModelImpl();
    }

    @Override
    public void login() {
        String userName = loginView.getUserName();
        String pwd = loginView.getPwd();
        loginView.showProgress();
        loginModel.login(userName, pwd, new OnLoginListener() {
            @Override
            public void loginSuccess(LoginResp resp) {
                loginView.hideProgress();
                loginView.goToMain();
            }

            @Override
            public void loginFailed() {
                loginView.hideProgress();
                loginView.clearUserName();
                loginView.clearPwd();
            }

            @Override
            public void unRegistered() {
                loginView.hideProgress();
                loginView.clearUserName();
                loginView.clearPwd();
            }
        });

        Observable.create(new Observable.OnSubscribe<LoginResp>() {

            @Override
            public void call(Subscriber<? super LoginResp> subscriber) {
                if (subscriber.isUnsubscribed())
                    return;
            }
        });

    }
}
